package com.ybzl.quartz.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//@Configuration
public class InfluxConfig {

    /**
     * 服务器url
     */
    @Value("${influx.url}")
    private String url;

    /**
     * API请求token
     */
    @Value("${influx.token}")
    private String token;

    /**
     * 所属组织
     */
    @Value("${influx.org}")
    private String org;

    /**
     * 总数
     */
    @Value("${influx.maxTotal:8}")
    private int maxTotal;

    /**
     * 最大空闲数
     */
    @Value("${influx.maxIdle:8}")
    private int maxIdle;

    /**
     * 最小空闲数
     */
    @Value("${influx.minIdle:0}")
    private int minIdle;

    /**
     * 获取连接超时时间，单位 秒
     */
    @Value("${influx.maxWaitTime:20}")
    private long maxWaitTime;

    @Bean
    public InfluxPool influxPool() {
        InfluxFactory factory = new InfluxFactory(url, token,org);
        GenericObjectPoolConfig<InfluxConn> config =
                new GenericObjectPoolConfig<InfluxConn>();
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        //config.setMaxWait(Duration.ofSeconds(maxWaitTime));
        // 取消注册到 MBean中，避免和Redis连接池冲突
        // https://www.freesion.com/article/44131208663/
        config.setJmxEnabled(false);
        return new InfluxPool(factory, config);
    }
}
